package com.aptech.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BookValidator {

    public static boolean validateTitle(String title) {
        Pattern pattern;
        Matcher matcher;

        final String FULLNAME_PATTERN = "^[a-zA-Z0-9_ -#@]{1,50}$";

        pattern = Pattern.compile(FULLNAME_PATTERN);

        matcher = pattern.matcher(title);

        return matcher.matches();
    }

    public static boolean validatePrice(String price) {
        Pattern pattern;
        Matcher matcher;

        final String FULLNAME_PATTERN = "^\\d+(\\.{0,1}(\\d{0,2}?))?$";

        pattern = Pattern.compile(FULLNAME_PATTERN);

        matcher = pattern.matcher(price);

        return matcher.matches();
    }

    public static boolean validateAuthor(String author) {
        Pattern pattern;
        Matcher matcher;

        final String FULLNAME_PATTERN = "^[a-zA-Z0-9_ -#@]{1,50}$";

        pattern = Pattern.compile(FULLNAME_PATTERN);

        matcher = pattern.matcher(author);

        return matcher.matches();
    }

    public static boolean validatePagenums(String pagenums) {
        Pattern pattern;
        Matcher matcher;

        final String FULLNAME_PATTERN = "^[0-9]{1,7}$";

        pattern = Pattern.compile(FULLNAME_PATTERN);

        matcher = pattern.matcher(pagenums);

        return matcher.matches();
    }

    public static boolean validatePubdate(int day, int month, int year) {

        // Check if the year is a leap year
        boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);

        // month minus by 1 
        // because value in Calendar object from 0 to 11
        if ((month == 0 || month == 2 || month == 4
                || month == 6 || month == 7
                || month == 9 || month == 11)
                && day <= 31) {
            return true;

        } else if ((month == 3 || month == 5 || month == 8 || month == 10)
                && day <= 30) {
            return true;
        } else if (month == 1 && isLeapYear && day <= 29) {
            return true;
        } else if (month == 1 && !isLeapYear && day <= 28) {
            return true;
        } else {
            return false;
        }
    }
}
